package ve.com.wayoyo.wuml_case.DibujoDiagramas;

import java.util.ArrayList;

import ve.com.wayoyo.wuml_case.R;
import ve.com.wayoyo.wuml_case.UserPref;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class NuevoMetodoComponente extends Dialog {

	private TextView texto;
	private EditText nombreMetodo;
	private EditText retornoMetodo;
	private EditText nombreVariable;
	private EditText tipoDatoEntrada;
	private String alcance;
	private boolean met_final;
	private boolean met_static;
	private boolean atr_final;
	private LinearLayout _metodo;
	private LinearLayout layoutTemp;
	private LinearLayout _layoutIn;
	private CheckBox cb_final; 
	private ArrayList<CompVariable> listaAtributosEntrada= new ArrayList<CompVariable>();

	public NuevoMetodoComponente(Context context) {
		super(context);
	}	

	@Override
	public void onCreate(Bundle savedInstanceState) {	
		super.onCreate(savedInstanceState);
		setContentView(R.layout.metodo_componente_layout);
		int pageWidth = UserPref.getInstancia().getWinWidth();
		int pageHeight = UserPref.getInstancia().getWinHeight();
		LayoutParams params = getWindow().getAttributes();
		if (pageWidth<750){
			params.width = pageWidth;
			params.height = pageHeight;
		}else{
			params.width = 750;
			params.height = 400;   
		}		     
		getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);

		setTitle("Nuevo Metodo");
		_metodo = (LinearLayout) findViewById(R.id.metodosLayout);		
		_metodo.removeAllViewsInLayout();
		_layoutIn = new LinearLayout(getContext());
		_layoutIn.setOrientation(LinearLayout.VERTICAL);		
		this.setNombreMetodo();
		this.setAlcanceMetodo();
		this.setVariableRetorno();
		this.setVariablesEntrada();
		_metodo.addView(_layoutIn);
		this.setBotonGuardar();
	}

	public void alert(String mensaje) {
		Toast.makeText(this.getContext(), mensaje, Toast.LENGTH_SHORT).show();
	}
	
	private void setAlcanceMetodo() {
		layoutTemp = new LinearLayout(getContext());
		texto  = new TextView(getContext());
		texto.setText("Modificadores");
		texto.setTextSize(20);
		_metodo.addView(texto);		
		this.setModificadoresMetodos();
	}

	private void setModificadoresMetodos() {

		final CheckBox cb_final = new CheckBox(getContext());
		cb_final.setText("final");
		final CheckBox cb_static = new CheckBox(getContext());
		cb_static.setText("static");
		final CheckBox cb_abstract = new CheckBox(getContext());		
		cb_abstract.setText("abstract");
		cb_abstract.setTextColor(Color.GRAY);
		cb_abstract.setClickable(false);
		cb_final.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked) {
					met_final = true;
				}
				else {
					met_final = false;
				}
			}
		});

		cb_static.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked) {
					met_static = true;
				}
				else {
					met_static = false;
				}
			}
		});	


		RadioGroup rg_alcance = new RadioGroup(getContext());
		rg_alcance.setOrientation(RadioGroup.HORIZONTAL);
		RadioButton rb_public = new RadioButton(getContext());
		rb_public.setId(R.id.radio_button_public);
		rb_public.setText("Public");
		RadioButton rb_private = new RadioButton(getContext());
		rb_private.setId(R.id.radio_button_private);
		rb_private.setText("Private");
		RadioButton rb_protected = new RadioButton(getContext());
		rb_protected.setId(R.id.radio_button_protected);
		rb_protected.setText("Protected");
		RadioButton rb_default = new RadioButton(getContext());
		rb_default.setText("Default");		
		rg_alcance.addView(rb_default);
		rg_alcance.addView(rb_public);
		rg_alcance.addView(rb_private);
		rg_alcance.addView(rb_protected);
		rb_default.setChecked(true);
		alcance = "default";
		rg_alcance.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				if (checkedId == R.id.radio_button_public)
					alcance = "public";
				else if (checkedId == R.id.radio_button_private)
					alcance = "private";
				else if (checkedId == R.id.radio_button_protected)
					alcance = "protected";
				else
					alcance = "default";
			}
		});
		_metodo.addView(rg_alcance);
		layoutTemp.addView(cb_final);
		layoutTemp.addView(cb_static);	
		layoutTemp.addView(cb_abstract);
		_metodo.addView(layoutTemp);
	}

	private void setNombreMetodo(){
		layoutTemp = new LinearLayout(getContext());
		texto = new TextView(getContext());
		texto.setText("Nombre");	
		texto.setTextSize(16);
		layoutTemp.addView(texto);		
		this.setNombreMetodoEdit();
		_metodo.addView(layoutTemp);
	}

	private void setNombreMetodoEdit(){
		nombreMetodo = new EditText(getContext());
		nombreMetodo.setHint("Nombre del Metodo");
		nombreMetodo.setMinimumWidth(150);
		layoutTemp.addView(nombreMetodo);
	}

	private void setVariableRetorno(){
		layoutTemp = new LinearLayout(getContext());
		texto = new TextView(getContext());
		texto.setText("Variable de Retorno");
		texto.setTextSize(16);
		layoutTemp.addView(texto);		
		this.setVariableRetornoEdit();
		_metodo.addView(layoutTemp);
	}

	private void setVariableRetornoEdit(){
		retornoMetodo = new EditText(getContext());		
		retornoMetodo.setHint("Tipo de retorno");
		retornoMetodo.setMinimumWidth(150);			
		layoutTemp.addView(retornoMetodo);
	}

	private void setVariablesEntrada(){
		texto = new TextView(getContext());
		texto.setText("Variables de Entrada");
		texto.setTextSize(20);
		_metodo.addView(texto);
		this.setNewVariablesEntrada();
		_metodo.addView(layoutTemp);
	}

	private void setNewVariablesEntrada(){
		layoutTemp = new LinearLayout(getContext());

		texto = new TextView(getContext());
		texto.setText("Nombre Atributo");
		texto.setTextSize(16);
		layoutTemp.addView(texto);

		this.setNombreVariableEntrada();

		texto = new TextView(getContext());
		texto.setText("Tipo de Dato");
		texto.setTextSize(16);
		layoutTemp.addView(texto);

		this.setTipoDatoEntrada();

		this.setfinal();

		Button buttonOK = new Button(getContext());
		buttonOK.setText("Agregar");
		buttonOK.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {				
				String nombreVariabletexto = nombreVariable.getText().toString();
				String tipoDatotexto = tipoDatoEntrada.getText().toString();
				if ((nombreVariabletexto.equals(""))||(tipoDatotexto.equals(""))){
					tipoDatoEntrada.setText("");
					nombreVariable.setText("");
					cb_final.setChecked(false);
					tipoDatoEntrada.setHint("Int");
					nombreVariable.setHint("Nombre del Atributo");
					alert("Debe indicar el nombre de variable y el tipo de dato");
				}else{
					CompVariable variable = new CompVariable(nombreVariabletexto,tipoDatotexto);
					variable.setFinal(atr_final); 
					cb_final.setChecked(false);
					listaAtributosEntrada.add(variable);

					tipoDatoEntrada.setText("");
					nombreVariable.setText("");
				}
				if (listaAtributosEntrada.size()>0)
				{
					NuevoMetodoComponente.this.listaVariablesEntrada();
				}
			}
		});
		layoutTemp.addView(buttonOK);
	}

	private void setfinal() {
		cb_final = new CheckBox(this.getContext());        
		cb_final.setText("final");
		cb_final.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked) {
					atr_final = true;
				}
				else {
					atr_final = false;
				}
			}
		});
		layoutTemp.addView(cb_final);

	}

	protected void listaVariablesEntrada() {
		int pos = listaAtributosEntrada.size()-1;
		layoutTemp = new LinearLayout(getContext());		

		TextView nombreAtributo = new TextView(getContext());
		nombreAtributo.setText("Nombre Atributo: ");
		layoutTemp.addView(nombreAtributo);

		this.setNombreVariableLista(listaAtributosEntrada.get(pos));

		TextView tipoDatoText = new TextView(getContext());
		tipoDatoText.setText("Tipo de Dato: ");
		layoutTemp.addView(tipoDatoText);
		this.setTipoDatoEditLista(listaAtributosEntrada.get(pos));

		this.setFinalLista(listaAtributosEntrada.get(pos));
		_layoutIn.addView(layoutTemp);

	}

	private void setFinalLista(CompVariable compVariable) {
		cb_final = new CheckBox(this.getContext());        
		cb_final.setText("final");
		cb_final.setChecked(compVariable.isFinal());
		cb_final.setClickable(false);
		cb_final.setTextColor(Color.GRAY);
		layoutTemp.addView(cb_final);
	}

	private void setTipoDatoEditLista(CompVariable CompVariable) {
		texto = new TextView(getContext());
		texto.setText(CompVariable.getTipoDato());
		texto.setMinimumWidth(100);
		layoutTemp.addView(texto);

	}

	private void setNombreVariableLista(CompVariable CompVariable) {
		texto = new TextView(getContext());
		texto.setText(CompVariable.getNombre());
		texto.setMinimumWidth(100);
		layoutTemp.addView(texto);

	}	

	private void setTipoDatoEntrada() {
		tipoDatoEntrada = new EditText(getContext());
		tipoDatoEntrada.setHint("Int");
		tipoDatoEntrada.setMinimumWidth(150);
		layoutTemp.addView(tipoDatoEntrada);
	}

	private void setNombreVariableEntrada() {
		nombreVariable = new EditText(getContext());		
		nombreVariable.setHint("Nombre del Atributo");		
		layoutTemp.addView(nombreVariable);	
	}

	private void setBotonGuardar(){
		layoutTemp = new LinearLayout(getContext());
		Button buttonOK = new Button(getContext());
		buttonOK.setText("Guardar");
		LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		buttonOK.setLayoutParams(params );
		buttonOK.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {				
				String nombre = nombreMetodo.getText().toString();
				String retorno = retornoMetodo.getText().toString();
				CompMetodo metodo = new CompMetodo(nombre, alcance);
				metodo.set_isFinal(met_final);
				metodo.set_isStatic(met_static);
				metodo.setTipoDato(retorno);
				metodo.setVariablesEntrada(listaAtributosEntrada);
				((CompClase) VistaDiagrama.getVista().getDiagrama().getSelectedItem()).addMetodo(metodo);
				NuevoMetodoComponente.this.dismiss();
			}
		});
		layoutTemp.addView(buttonOK);
		_metodo.addView(layoutTemp);
	}

}
